package store

import (
	"cluster-cache/common"
	"cluster-cache/meta"
	"cluster-cache/model"
	"github.com/cespare/xxhash/v2"
)

// MemoryStore 内存数据
type MemoryStore struct {
	KV      model.KeyValue
	SS      model.SortSet
	Indexes *BucketIndex
}

func (m *MemoryStore) Apply(index uint64, id int, kvs *common.KeyValueScore) {
	if m.KV == nil || m.SS == nil {
		return
	}
	m.Indexes.Update(xxhash.Sum64(kvs.Key), index)
	switch kvs.Id {
	case 1:
		m.KV.Add(kvs.Key, kvs.Value)
		break
	case 2:
		m.KV.Delete(kvs.Key)
		break
	case 3:
		m.SS.Add(string(kvs.Key), kvs.Score, string(kvs.Value))
		break
	case 4:
		m.SS.Delete(string(kvs.Key), string(kvs.Value))
		break
	}
	// reuse kvs
	meta.KvPool.Put(kvs)
}
